//
//  CLiveListCell.m
//  ACLiteAVUISDK-ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/3/29.
//

#import "ACLiveListCell.h"

@interface ACLiveListCell ()

@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIImageView *liveCoverImage;
@property (weak, nonatomic) IBOutlet UIImageView *liveStatusIcon;
@property (weak, nonatomic) IBOutlet UILabel *liveTitleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *liverHeadIcon;
@property (weak, nonatomic) IBOutlet UILabel *liverNameLabel;
@property (weak, nonatomic) IBOutlet UIView *liveTypeBgView;
@property (weak, nonatomic) IBOutlet UIView *liveNumBgView;

@property (weak, nonatomic) IBOutlet UILabel *liveTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *liveNumLabel;

@property (weak, nonatomic) IBOutlet UIButton *toAppointmentBtn;


@end


@implementation ACLiveListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.bgView.layer.masksToBounds = YES;
    self.bgView.layer.cornerRadius = 10;
    
    self.toAppointmentBtn.layer.masksToBounds = YES;
    self.toAppointmentBtn.layer.cornerRadius = 16;
    self.toAppointmentBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    
    self.liveNumBgView.layer.masksToBounds = YES;
    self.liveNumBgView.layer.cornerRadius = 4;
    
    self.liveTypeBgView.layer.masksToBounds = YES;
    self.liveTypeBgView.layer.cornerRadius = 4;
    
    self.liverHeadIcon.layer.masksToBounds = YES;
    self.liverHeadIcon.layer.cornerRadius = 10;
    
    self.liveCoverImage.backgroundColor = [UIColor grayColor];
    self.liverHeadIcon.backgroundColor = [UIColor cyanColor];
    
    @weakify(self);
    [[self.toAppointmentBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if (self.bookActionHandler) {
            self.bookActionHandler();
        }
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setLiveModel:(ACLiveModel *)liveModel{
    _liveModel = liveModel;
    NSString *coverStr = _liveModel.img;
    if ((![coverStr startsWith:@"http://"] && ![coverStr startsWith:@"https://"] )&& coverStr != nil) {
        coverStr = [@"http://" stringByAppendingString:coverStr];
    }
    NSURL *coverURL = [NSURL URLWithString:coverStr];
    [self.liveCoverImage sd_setImageWithURL:coverURL];
    self.liveTitleLabel.text = _liveModel.title;
    ACLiveCreaterModel *userModel = _liveModel.user;
    self.liverNameLabel.text = userModel.nickname;
    NSString *headIconStr = userModel.avatar;
    if ((![headIconStr startsWith:@"http://"] && ![headIconStr startsWith:@"https://"]) && headIconStr != nil) {
        headIconStr = [@"http://" stringByAppendingString:headIconStr];
    }
    [self.liverHeadIcon sd_setImageWithURL:[NSURL URLWithString:headIconStr]];
    if ([_liveModel.booked.user_id isEqualToString:[[ACUserProfileManager shareInstance] userModel].user_id]) {
        [self.toAppointmentBtn setEnabled:NO];
        [self.toAppointmentBtn setTitle:@"已预约" forState:UIControlStateDisabled];
        [self.toAppointmentBtn setBackgroundColor:UIColorFromRGB(0xFEAFA3)];
        [self.toAppointmentBtn setImage:[[UIImage alloc] init] forState:UIControlStateDisabled];
    }else{
        [self.toAppointmentBtn setEnabled:YES];
        [self.toAppointmentBtn setTitle:@"预约" forState:UIControlStateNormal];
        [self.toAppointmentBtn setBackgroundColor:UIColorFromRGB(0xFF6048)];
        [self.toAppointmentBtn setImage:[UIImage imageWithName:@"icon_live_alert" bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateNormal];
    }
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"直播类型 " attributes:@{
        NSFontAttributeName:[UIFont systemFontOfSize:12.0],NSForegroundColorAttributeName:UIColorFromRGB(0x606266)
    }];
    [attributedStr appendAttributedString: [[NSAttributedString alloc] initWithString:@"路演直播" attributes:@{
        NSFontAttributeName:[UIFont systemFontOfSize:12.0],NSForegroundColorAttributeName:UIColorFromRGB(0xFF6048)
    }]];
    self.liveTypeLabel.attributedText = attributedStr;
    
    int status = [_liveModel.status intValue];
    if (status == 1) {// 未开始
        self.liveStatusIcon.image = [UIImage imageWithName:@"icon_live_status_nostart" bundle:ACLiteAVUISDK targetClass:[self class]];
        [self.toAppointmentBtn setTitle:@"预约" forState:UIControlStateNormal];
        [self.toAppointmentBtn setImage:[UIImage imageWithName:@"icon_live_alert" bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateNormal];
        [self.toAppointmentBtn setHidden:NO];
        [self.toAppointmentBtn setBackgroundColor:UIColorFromRGB(0xFF6048)];
    }else if (status == 3){ // 直播中
        [self.toAppointmentBtn setEnabled:YES];
        self.liveStatusIcon.image = [UIImage imageWithName:@"icon_live_status_lliving" bundle:ACLiteAVUISDK targetClass:[self class]];
        [self.toAppointmentBtn setHidden:NO];
        [self.toAppointmentBtn setBackgroundColor:UIColorFromRGB(0xFF6048)];
        [self.toAppointmentBtn setTitle:@"观看" forState:UIControlStateNormal];
        [self.toAppointmentBtn setImage:[[UIImage alloc] init] forState:UIControlStateNormal];
    }else if (status == 4){//已结束
        self.liveStatusIcon.image = [UIImage imageWithName:@"icon_live_status_end" bundle:ACLiteAVUISDK targetClass:[self class]];
        [self.toAppointmentBtn setHidden:YES];
    }
}

@end
